package cn.jsprun.service.posts;

import java.util.List;
import java.util.Map;

import cn.jsprun.dao.posts.AttachmentsDao;
import cn.jsprun.dao.posts.PostsDao;
import cn.jsprun.domain.Attachments;
import cn.jsprun.domain.Posts;
import cn.jsprun.struts.form.PostsForm;
import cn.jsprun.struts.form.PostsPageForm;
import cn.jsprun.struts.form.posts.ModrepliesPageForm;
import cn.jsprun.struts.form.posts.PruneForm;
import cn.jsprun.utils.BeanFactory;
import cn.jsprun.utils.Common;

public class PostsService {
	public PostsPageForm fidnByForums(PostsForm postsForm) {
		String displayorder = "";
		if (postsForm.getFilter().equals("normal")) {
			displayorder = "-2";
		}
		if (postsForm.getFilter().equals("ignore")) {
			displayorder = "-3";
		}
		return ((PostsDao) BeanFactory.getBean("postsDao")).fidnByForums(postsForm.getFid(), displayorder);
	}
	public boolean deletePostsIDArray(List deleteList) {

		if (deleteList != null && deleteList.size() > 0) {
			((PostsDao) BeanFactory.getBean("postsDao")).deletePostsIDArray(deleteList);
		}
		return true;
	}
	public boolean ignorePostsIDArray(List ignoreList) {

		if (ignoreList != null && ignoreList.size() > 0) {
			return ((PostsDao) BeanFactory.getBean("postsDao")).ignorePostsIDArray(ignoreList);
		}
		return true;
	}
	public List validatePostsIDArray(List validateList) {

		if (validateList != null && validateList.size() > 0) {
			return ((PostsDao) BeanFactory.getBean("postsDao")).validatePostsIDArray(validateList);
		}
		return null;
	}

	public Posts getPostsById(Integer pid){
		return ((PostsDao) BeanFactory.getBean("postsDao")).getPostsById(pid);
	}
	public ModrepliesPageForm fidnByModreplies(PostsForm postsForm) {
		String invisible = "";
		if (postsForm.getFilter().equals("normal")) {
			invisible="-2";
		}
		if (postsForm.getFilter().equals("ignore")) {
			invisible="-3";
		}
		return ((PostsDao) BeanFactory.getBean("postsDao")).fidnByModreplies(postsForm.getFid(),invisible);
	}
	public boolean validateModrepliesIDArray(List<Posts> modrepliesList) {

		if (modrepliesList != null && modrepliesList.size() > 0) {
			return ((PostsDao) BeanFactory.getBean("postsDao")).validateModrepliesIDArray(modrepliesList);
		}
		return true;
	}
	public boolean ignoreModrepliesIDArray(List<String> modrepliesList) {
		if (modrepliesList != null && modrepliesList.size() > 0) {
			return ((PostsDao) BeanFactory.getBean("postsDao")).ignoreModrepliesIDArray(modrepliesList);
		}
		return true;
	}
	public boolean deleteModrepliesIDArray(List<String> modrepliesList) {
		if (modrepliesList.size() <= 0) {
			return true;
		} else {
			((PostsDao) BeanFactory.getBean("postsDao")).deleteModrepliesIDArray(modrepliesList);
		}
		return true;
	}
	public boolean deletePostsByUserName(String userName) {
		return ((PostsDao) BeanFactory.getBean("postsDao")).deletePostsByUserName(userName);
	}
	public Map batchPrune(String sb, boolean b) {
		return ((PostsDao) BeanFactory.getBean("postsDao")).batchPrune(sb, b);
	}
	public Integer batchForumrecommend(StringBuffer deletesb, List list) {
		PostsDao postsDao = (PostsDao) BeanFactory.getBean("postsDao");
		postsDao.deleteForumrecommend(deletesb);
		int num=postsDao.updateForumrecommendList(list);
		postsDao=null;
		return num;
	}
	
	public List<Posts> getPostsListByPidList(List<Integer> pidList){
		return ((PostsDao) BeanFactory.getBean("postsDao")).getPostsListByPidList(pidList);
	}
	public int findPostCountByHql(String hql){
		return ((PostsDao) BeanFactory.getBean("postsDao")).findPostCountByHql(hql);
	}
	public List<Posts> findPostByhql(String hql,int start,int max){
		return ((PostsDao) BeanFactory.getBean("postsDao")).findPostByhql(hql, start, max);
	}
	public Posts findPostByThreadId(int tid){
		return ((PostsDao) BeanFactory.getBean("postsDao")).findPostByThreadId(tid);
	}
	public boolean updatePosts(Posts post){
		return ((PostsDao) BeanFactory.getBean("postsDao")).updatePosts(post);
	}
	public int insertAttachments(Attachments attachments){
		return ((AttachmentsDao) BeanFactory.getBean("attachmentsDao")).insertAttachments(attachments);
	}
	public int saveOrupdatePosts(Posts post){
		return ((PostsDao) BeanFactory.getBean("postsDao")).saveOrupdatePosts(post);
	}
	public boolean updateAttachments(Attachments attachments){
		return ((AttachmentsDao) BeanFactory.getBean("attachmentsDao")).updateAttachments(attachments);
	}
	public boolean deleteAttachments(Attachments attachments){
		return ((AttachmentsDao) BeanFactory.getBean("attachmentsDao")).deleteAttachments(attachments);
	}
	public Attachments findAttachmentsById(int aid){
		return ((AttachmentsDao) BeanFactory.getBean("attachmentsDao")).findAttachmentsById(aid);
	}
	public List<Posts> getPostsListByTid(Integer tid){
		return ((PostsDao) BeanFactory.getBean("postsDao")).getPostsListByTid(tid);
	}
	public String prunsql(PruneForm pf,int timeoffset){
		StringBuffer sql = new StringBuffer("from jrun_posts as p left join jrun_forums f on p.fid=f.fid left join jrun_threads t on p.tid=t.tid ");
		String and = "";
		String where = " where ";
		String userip = pf.getUseip();
		String keywords = pf.getKeywords();
		if (pf.getUseip() != null) {
			userip = pf.getUseip().replace("*", "%");
		}
		if (pf.getKeywords() != null) {
			while (keywords.indexOf("{") != -1&& keywords.indexOf("}") != -1&& keywords.indexOf("{") < keywords.indexOf("}")) {
				keywords = keywords.replace(keywords.substring(keywords.indexOf("{"), keywords.indexOf("}") + 1), "%");
			}
		}
		if (pf.getKeywords() != null && !pf.getKeywords().equals("")) {
			Object[] key = keywords.split(",");
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			sql.append("( p.message like '%"+key[0].toString().trim()+"%'");
			if(key.length>1){
				for (int i = 1; i < key.length; i++) {
					sql.append(" or p.message like '%"+key[i].toString().trim()+"%'");
				}
			}
			sql.append(")");
		}
		if (pf.getUsers() != null && !pf.getUsers().equals("")) {
			String cins ="" ;
			if (pf.getCins()==1) {
				cins = " binary ";
			} else {
				cins = "";
			}
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			String[] userNames = pf.getUsers().split(",");
			sql.append("(" +cins + " p.author ='" + userNames[0] + "' ");
			if(userNames.length>1){
				for (int i = 1; i < userNames.length; i++) {
				sql.append(" or "+cins+" p.author = '" + userNames[i] + "' ");
				}
			}
			sql.append(")");
		}
		if (userip != null && !userip.equals("")) {
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			sql.append("p.userip like "+userip);
		}
		if (pf.getEndtime() != null && !pf.getEndtime().equals("")&& Common.datecheck(pf.getEndtime())) {
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			int endtime = Common.dataToInteger(pf.getEndtime(),"yyyy-MM-dd")-timeoffset;
			sql.append("p.dateline<="+endtime);
		}
		if (pf.getStarttime() != null && !pf.getStarttime().equals("") && Common.datecheck(pf.getStarttime())) {
			int startime = Common.dataToInteger(pf.getStarttime(),"yyyy-MM-dd")-timeoffset;
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			sql.append("p.dateline>="+startime);
		}
		if (pf.getFid()!=null) {
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			sql.append("p.fid in ( "+pf.getFid()+" )");
		}else if(pf.getForums()>0){
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			sql.append("p.fid = "+pf.getForums());
		}
		if (pf.getLengthlimit() != null && !pf.getLengthlimit().equals("")) {
			sql.append(where);
			where = "";
			sql.append(and);
			and = " and ";
			sql.append("length(message) >"+pf.getLengthlimit());
		}
		return sql.toString();
	}
}
